#include "flappybird.h"
#include "ui_flappybird.h"
#include <QKeyEvent>
#include <QPushButton>
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QPoint>
#include <QColor>
#include <QDebug>
#include <QMessageBox>
#include <QGraphicsItem>

flappybird::flappybird(QWidget *parent):QMainWindow(parent),ui(new Ui::flappybird)
{
    ui->setupUi(this);

    button = new QPushButton("点击开始游戏",this);
    button->setGeometry(500,650,200,50);
    //设定开始游戏的按钮
    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));

    button2 = new QPushButton("速度：慢",this);
    button2->setGeometry(20,80,100,50);
    connect(button2,SIGNAL(clicked()),this,SLOT(slotStart2()));

    button3 = new QPushButton("难度：简单",this);
    button3->setGeometry(150,80,100,50);
    connect(button3,SIGNAL(clicked()),this,SLOT(slotStart3()));


    ran1[0]=qrand()%600;
    ran1[1]=qrand()%600;
    ran1[2]=qrand()%600;
    ran1[3]=qrand()%600;
    ran2[0]=qrand()%100;
    ran2[1]=qrand()%100;
    ran2[2]=qrand()%100;
    ran2[3]=qrand()%100;

}

void flappybird::setl()
{

    if(l1<-80)
    {
        ran1[0]=qrand()%600;
        ran2[0]=qrand()%100;
        l1=1600+qrand()%30-qrand()%30;
    }
    if(l2<-80)
    {
        ran1[1]=qrand()%600;
        ran2[1]=qrand()%100;
        l2=1600+qrand()%30-qrand()%30;
    }
    if(l3<-80)
    {
        ran1[2]=qrand()%600;
        ran2[2]=qrand()%100;
        l3=1600+qrand()%30-qrand()%30;
    }
    if(l4<-80)
    {
        ran1[3]=qrand()%600;
        ran2[3]=qrand()%100;
        l4=1600+qrand()%30-qrand()%30;
    }
    //设定每一根柱子的高度随机数与距离随机数，并在移动到最左边后返回右边
    if(l5<-100)
    {
        ranl5h=400+qrand()%200-qrand()%200;
        l5=2800+qrand()%100-qrand()%100;
        bscount=0;


    }
    //随机宝石
    if(l6<-100)
    {
        ranl6h=400+qrand()%200-qrand()%200;
        l6=4000+qrand()%100-qrand()%100;
        zscount=0;


    }
    //随机钻石

    update();

}

void flappybird::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setFont(QFont("微软雅黑",20,700,false));
    p.setBrush(Qt::lightGray);
    p.drawRect(0,0,1200,800);

    setl();

    if(time&&count==1)
    {
        l1-=velocity;
        l2-=velocity;
        l3-=velocity;
        l4-=velocity;
        l5-=velocity;
        l6-=velocity;
    }

    //八根柱子和宝石钻石的移动速度

QColor colorzhuzi(110,110,110);
p.setBrush(colorzhuzi);
int k1=ran1[0]-200+ran2[0]+hard;
int k2=ran1[0]-500-ran2[0]-hard;

int k3=ran1[1]-200+ran2[1]+hard;
int k4=ran1[1]-500-ran2[1]-hard;

int k5=ran1[2]-200+ran2[2]+hard;
int k6=ran1[2]-500-ran2[2]-hard;

int k7=ran1[3]-200+ran2[3]+hard;
int k8=ran1[3]-500-ran2[3]-hard;
//随机化八根柱子的高度


p.drawRect(l1,0,60,k1);
p.drawRect(l1,800,60,k2);

p.drawRect(l2,0,60,k3);
p.drawRect(l2,800,60,k4);

p.drawRect(l3,0,60,k5);
p.drawRect(l3,800,60,k6);

p.drawRect(l4,0,60,k7);
p.drawRect(l4,800,60,k8);
//画八根柱子

p.setBrush(Qt::yellow);
if(bscount==0)
p.drawEllipse(QPoint(l5,ranl5h),25,25);
//画宝石
if((l5-200)*(l5-200)+(ranl5h-F.y-20)*(ranl5h-F.y-20)<2809&&bscount==0)
{
    score+=5*hardship;
    bscount=1;
}


p.setBrush(Qt::blue);
if(zscount==0)
p.drawEllipse(QPoint(l6,ranl6h),10,10);
//画钻石
if((l6-200)*(l6-200)+(ranl6h-F.y-20)*(ranl6h-F.y-20)<1444&&zscount==0)
{
    score+=10*hardship;
    zscount=1;
}




p.drawText(QPoint(950,60),"生存时间: "+QString::number(time/1000)+"秒");
//计时器文本
p.drawText(QPoint(20,60),"分数: "+QString::number(score));
//分数文本

if(180>l1+60&&scorecount==0)
{score+=1*hardship;
scorecount=1;
}
if(180>l2+60&&scorecount==1)
{score+=1*hardship;
scorecount=2;
}
if(180>l3+60&&scorecount==2)
{score+=1*hardship;
scorecount=3;
}
if(180>l4+60&&scorecount==3)
{score+=1*hardship;
scorecount=0;
}


    display();
    die();

    if(count==1)
    {
        F.v+=F.g;
        F.y-=F.v;
        p.setBrush(Qt::red);
        p.drawRect(180,F.y,40,40);
        button->hide();
    }
    //bird的移动与绘画

    if((F.y<k1&&180>l1&&180<l1+60&&count==1)||(F.y<k1&&220>l1&&220<l1+60&&count==1)||(F.y<k3&&180>l2&&180<l2+60&&count==1)||(F.y<k3&&220>l2&&220<l2+60&&count==1)||(F.y<k5&&180>l3&&180<l3+60&&count==1)||(F.y<k5&&220>l3&&220<l3+60&&count==1)||(F.y<k7&&180>l4&&180<l4+60&&count==1)||(F.y<k7&&220>l4&&220<l4+60&&count==1))
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你坚持了"+QString::number(time/1000)+"秒，得分为"+QString::number(score));
        button->setText("点击开始游戏");
        button->show();
    }//上面两顶点碰到柱子则死亡


    if((F.y+40-k2>800&&180>l1&&180<l1+60&&count==1)||(F.y+40-k2>800&&220>l1&&220<l1+60&&count==1)||(F.y+40-k4>800&&180>l2&&180<l2+60&&count==1)||(F.y+40-k4>800&&220>l2&&220<l2+60&&count==1)||(F.y+40-k6>800&&180>l3&&180<l3+60&&count==1)||(F.y+40-k6>800&&220>l3&&220<l3+60&&count==1)||(F.y+40-k8>800&&180>l4&&180<l4+60&&count==1)||(F.y+40-k8>800&&220>l4&&220<l4+60&&count==1))
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你坚持了"+QString::number(time/1000)+"秒，得分为"+QString::number(score));
        button->setText("点击开始游戏");
        button->show();
    }//下面两顶点碰到柱子则死亡

}

void flappybird::keyPressEvent(QKeyEvent *event)
{
    if(!state){
        return ;
    }
    switch (event->key()) {
    case Qt::Key_A:
        F.v=0.8;//按A使得速度立刻变为0.8
        break;
    default:
        return;//忽略其他按钮
    }
    update();

}
void flappybird::die()
{
    if((F.y<0||F.y>760)&&diecount==0)
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你坚持了"+QString::number(time/1000)+"秒，得分为"+QString::number(score));
        button->setText("点击开始游戏");
        button->show();

    }//越界死亡
}

void flappybird::display()
{
if(count==1)
time+=time1.restart();
//计时器
update();
}
void flappybird::slotStart()
{
    l1=1200;
    l2=1600;
    l3=2000;
    l4=2400;
    l5=3000;
    l6=4200;
    time=0;
    score=0;
    scorecount=0;
    zscount=0;
    bscount=0;
    QMessageBox::about(this,"游戏规则","按A键进行跳跃，躲避深灰色的柱子加1分，碰到金色宝石加5分，碰到蓝色钻石加10分,中等难度分数1.2倍，困难难度分数1.5倍");
    QMessageBox::about(this,"友情提示","宝石与钻石，既是机遇，又是陷阱");
    time1.elapsed();
    time=0;
    ran1[0]=qrand()%600;
    ran1[1]=qrand()%600;
    ran1[2]=qrand()%600;
    ran1[3]=qrand()%600;
    ran2[0]=qrand()%100;
    ran2[1]=qrand()%100;
    ran2[2]=qrand()%100;
    ran2[3]=qrand()%100;
    F.y=400;
    F.v=0.9;
    count=1;
    diecount=0;
    time1.start();

    button->setText("重新游戏");
    state=true;


    //开始游戏/重新开始游戏 重置所有数值

    update();

}
void flappybird::slotStart2()
{
    if(button2->text()=="速度：中"&&count!=1)
    {velocity=0.52;
    button2->setText("速度：快");
    }
    else if(button2->text()=="速度：快"&&count!=1)
    {velocity=0.29;
    button2->setText("速度：慢");
    }
    else if(button2->text()=="速度：慢"&&count!=1)
    {velocity=0.4;
    button2->setText("速度：中");
    }
    update();
}
void flappybird::slotStart3()
{
    if(button3->text()=="难度：简单"&&count!=1)
    {
        hard=30;
        hardship=1.2;
        button3->setText("难度：中等");
    }
    else if(button3->text()=="难度：中等"&&count!=1)
    {
        hard=60;
        hardship=1.5;
        button3->setText("难度：困难");
    }
    else if(button3->text()=="难度：困难"&&count!=1)
    {
        hard=0;
        hardship=1;
        button3->setText("难度：简单");
    }
    update();
}

flappybird::~flappybird()
{
      delete ui;
}
